package com.cxb.view;

import com.cxb.entity.Gsensor;
import com.cxb.utils.SharedPreferencesUtils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class GsensorBallView extends View implements SensorEventListener {

	private final static float TRIANGLE_ANGLE = 70;// ����νǶ�
	private final static float BALL_SIZE = 30;// Բ���С
	private final static float PROGRESS_AREA_SIZE = 250;// �������Ĵ�С
	private final static float STEP_HEIGHT = 8;// ÿ�����ݵĸ߶�
	private final static float LINE_NUM = 10;// ÿ�����������
	private final static float MAX_G_SENSOR_VALUE = 8;// ÿ�����������ֵ
	private int centerX;
	private int centerY;

	private float leftStep = 2;
	private float rightStep = 3;
	private float topStep = 5;
	private float bottomStep = 10;

	private float ballX;
	private float ballY;

	private double leftMax = 0.00;
	private double rightMax = 0.00;
	private double topMax = 0.00;
	private double bottomMax = 0.00;
	private SensorManager sensorManager;
	private SharedPreferencesUtils sharedPreferencesUtils;

	public GsensorBallView(Context context, AttributeSet attrs) {
		super(context, attrs);
		sharedPreferencesUtils = new SharedPreferencesUtils(context);
		sensorManager = (SensorManager) context
				.getSystemService(Context.SENSOR_SERVICE);
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_UI);
	}

	int count_decelate = 0;
	int count_accelate = 0;
	int count_trun = 0;

	// public void updateGsensorStatus(Gsensor gsensor) {
	// invalidate();
	// }

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		centerX = this.getWidth() / 3;
		centerY = this.getHeight() / 2;
		ballX = centerX;
		ballY = centerY;
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	public void reInitMaxValue() {
		topMax = 0;
		bottomMax = 0;
		leftMax = 0;
		rightMax = 0;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			float x = event.getX();
			float y = event.getY();

			float left = centerX - PROGRESS_AREA_SIZE / 2;
			float right = centerX + PROGRESS_AREA_SIZE / 2;
			float top = centerY - PROGRESS_AREA_SIZE / 2;
			float bottom = centerY + PROGRESS_AREA_SIZE / 2;
			if (x > left && x < right && y > top && y < bottom) {
				x = x - centerX;
				y = centerY - y;
				// 上方区域
				if (y > 0
						&& y > Math.abs(x)
								/ Math.tan((TRIANGLE_ANGLE * Math.PI / 2) / 180)) {
					topMax = 0;
				} else if (y < 0
						&& -y > Math.abs(x)
								/ Math.tan((TRIANGLE_ANGLE * Math.PI / 2) / 180)) {
					bottomMax = 0;
				} else if (x > 0
						&& y < Math.abs(x)
								/ Math.tan((TRIANGLE_ANGLE * Math.PI / 2) / 180)) {
					rightMax = 0;
				} else if (x < 0
						&& -y < Math.abs(x)
								/ Math.tan((TRIANGLE_ANGLE * Math.PI / 2) / 180)) {
					leftMax = 0;
				}
				
				invalidate();
			}
		}

		return super.onTouchEvent(event);
	}

	@Override
	protected void onDraw(Canvas canvas) {

		drawTriagleArea(canvas);// �����������
		drawBall(canvas);
		drawMaxValue(canvas);
		drawGsensorStatus(canvas);
		super.onDraw(canvas);
	}

	private void drawGsensorStatus(Canvas canvas) {
		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setTextSize(20);
		int count_accelate = sharedPreferencesUtils.getGsensorAccCount();
		int count_decelate = sharedPreferencesUtils.getGsensorDecCount();
//		int count_trun_right = sharedPreferencesUtils
//				.getGsensorTurnRightCount();
//		int count_trun_left = sharedPreferencesUtils.getGsensorTurnLeftCount();
		canvas.drawText("急刹车:" + count_decelate + "次",
				(float) (centerX + PROGRESS_AREA_SIZE * 2.0 / 3.0), centerY
						- PROGRESS_AREA_SIZE / 3 + 20, paint);
		canvas.drawText("急加速:" + count_accelate + "次", centerX
				+ PROGRESS_AREA_SIZE * 2.0f / 3.0f, centerY
				- PROGRESS_AREA_SIZE / 3 + 50, paint);
		
		
		
	}

	private void drawMaxValue(Canvas canvas) {
		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setAntiAlias(true);
		paint.setTextSize(20);

		float textWidth = paint.measureText("0.00G");
		canvas.drawText(String.format("%.2f", topMax) + "", centerX - textWidth
				/ 2, centerY - PROGRESS_AREA_SIZE / 2 + textWidth / 2, paint);

		canvas.drawText(String.format("%.2f", bottomMax) + "", centerX
				- textWidth / 2, centerY + PROGRESS_AREA_SIZE / 2 - textWidth
				/ 3, paint);

		canvas.drawText(String.format("%.2f", leftMax) + "", centerX
				- PROGRESS_AREA_SIZE / 2, centerY, paint);

		canvas.drawText(String.format("%.2f", rightMax) + "", centerX
				- textWidth + PROGRESS_AREA_SIZE / 2, centerY, paint);
	}

	private void drawBall(Canvas canvas) {
		Paint normalPaint = new Paint();
		normalPaint.setColor(Color.WHITE);
		normalPaint.setAntiAlias(true);
		normalPaint.setStyle(Style.STROKE);
		normalPaint.setStrokeWidth(3);
		canvas.drawCircle(ballX, ballY, 10, normalPaint);
	}

	private void drawTriagleArea(Canvas canvas) {
		Paint normalPaint = new Paint();
		normalPaint.setColor(Color.RED);
		normalPaint.setAlpha(100);
		normalPaint.setAntiAlias(true);
		Paint hightLightPaint = new Paint();
		hightLightPaint.setColor(Color.parseColor("#FC875D"));
		hightLightPaint.setAntiAlias(true);
		// �������������
		for (int i = 0; i < LINE_NUM; i++) {
			Paint paint = (i - bottomStep) < 0 ? hightLightPaint : normalPaint;
			int top = (int) (centerY + BALL_SIZE / 2 + i
					* ((PROGRESS_AREA_SIZE / 2 - BALL_SIZE / 2.0) / LINE_NUM));
			int left = centerX
					- (int) ((Math.tan((TRIANGLE_ANGLE * Math.PI / 2) / 180) * (top - centerY)));
			canvas.drawRect(left, top, left + (centerX - left) * 2, top
					+ STEP_HEIGHT, paint);
		}
		// �������������
		for (int i = 0; i < LINE_NUM; i++) {
			Paint paint = (i - topStep) < 0 ? hightLightPaint : normalPaint;
			int top = (int) (centerY - BALL_SIZE / 2 - i
					* ((PROGRESS_AREA_SIZE / 2 - BALL_SIZE / 2.0) / LINE_NUM));
			int left = centerX
					- (int) ((Math.tan((TRIANGLE_ANGLE * Math.PI / 2) / 180) * (centerY - top)));
			canvas.drawRect(left, top - STEP_HEIGHT, left + (centerX - left)
					* 2, top, paint);
		}

		// �������������

		for (int i = 0; i < LINE_NUM; i++) {
			Paint paint = (i - leftStep) < 0 ? hightLightPaint : normalPaint;
			int left = (int) (centerX - BALL_SIZE / 2 - i
					* ((PROGRESS_AREA_SIZE / 2 - BALL_SIZE / 2.0) / LINE_NUM));
			int top = centerY
					- (int) ((Math.tan((TRIANGLE_ANGLE * Math.PI / 2) / 180) * (centerX - left)));
			canvas.drawRect(left - STEP_HEIGHT, top, left, top
					+ (centerY - top) * 2, paint);
		}

		// �������������

		for (int i = 0; i < LINE_NUM; i++) {
			Paint paint = (i - rightStep) < 0 ? hightLightPaint : normalPaint;
			int left = (int) (centerX + BALL_SIZE / 2 + i
					* ((PROGRESS_AREA_SIZE / 2 - BALL_SIZE / 2.0) / LINE_NUM));
			int top = centerY
					- (int) ((Math.tan((TRIANGLE_ANGLE * Math.PI / 2) / 180) * (left - centerX)));
			canvas.drawRect(left, top, left + STEP_HEIGHT, top
					+ (centerY - top) * 2, paint);
		}

	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent event) {

		float x = event.values[SensorManager.DATA_X];
		float y = event.values[SensorManager.DATA_Y];
		float z = event.values[SensorManager.DATA_Z];

		// Debug.printf("test", "x=" + x + " y=" + y + " z=" + z);
		updateGensorValue(y, z);
	}

	float preX;
	float preY;

	public void updateGensorValue(float x, float y) {

		float tempX = (float) (centerX + PROGRESS_AREA_SIZE / 2.0 * x
				/ MAX_G_SENSOR_VALUE);
		float tempY = (float) (centerY + PROGRESS_AREA_SIZE / 2.0 * y
				/ MAX_G_SENSOR_VALUE);
		if (Math.abs(preX - x) > 0.6) {
			ballX = tempX;
			preX = x;
		}
		if (Math.abs(preY - y) > 0.6) {
			ballY = tempY;
			preY = y;
		}

		int stepY = (int) (y * LINE_NUM / MAX_G_SENSOR_VALUE);
		int stepX = (int) (x * LINE_NUM / MAX_G_SENSOR_VALUE);
		stepX = (int) Math.min(stepX, LINE_NUM);
		stepY = (int) Math.min(stepY, LINE_NUM);

		if (stepX > 0) {
			rightStep = stepX;
			leftStep = 0;
			if (rightMax < x) {
				rightMax = x;
			}

		} else {
			rightStep = 0;
			leftStep = Math.abs(stepX);
			if (leftMax < -x) {
				leftMax = -x;
			}
		}

		if (stepY > 0) {
			bottomStep = stepY;
			topStep = 0;
			if (bottomMax < y) {
				bottomMax = y;
			}
		} else {
			bottomStep = 0;
			topStep = Math.abs(stepY);
			if (topMax < -y) {
				topMax = -y;
			}
		}
		invalidate();
	}

}
